﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SmartEP.Presenter;
using SmartEP.DTO;

namespace SmartDesktop
{
    public partial class LoginForm : Form, IUser
    {
        #region Fields
        private UserPresenter _persenter;
        #endregion

        public LoginForm()
        {
            InitializeComponent();
            _persenter = new UserPresenter(this);
        }

        #region Events
        private void frmUserLogin_Load(object sender, EventArgs e)
        {
            lblError.Text = "";

            LoadInfoLoginUser("Login.xml");
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (!IsNullTextBox())
            {
                _persenter.AuthenticateUser();
            }
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        #endregion

        #region Property and MeThods Interface
        public string UserName
        {
            get
            {
                return txtUsername.Text;
            }
            set
            {
                txtUsername.Text = value;
            }
        }

        public string Password
        {
            get
            {
                return txtPassword.Text;
            }
            set
            {
                txtPassword.Text = value;
            }
        }

        public void AuthenticateUser(User user)
        {
            if (user.CorrectPassword)
            {
                MainForm.CurrentUser = user;
                if (chkRemember.Checked)
                    SaveInfoLoginUser("Login.xml");

                this.Close();
            }
            else
            {
                lblError.Text = "Login Fail...";
            }
        }

        #endregion

        #region Private Methods
        private void SaveInfoLoginUser(string FileName)
        {
            _persenter.WriteLoginXML(FileName);
        }
        private void LoadInfoLoginUser(string FileName)
        {
            System.Collections.ArrayList arr= _persenter.ReadLoginXML(FileName);
            if(arr!=null)
                txtUsername.Text = Convert.ToString(arr[0]);
        }
        private bool IsNullTextBox()
        {
            if (txtUsername.Text.Length == 0)
            {
                MessageBox.Show("UserName not null or not empty");
                txtUsername.Focus();
                return true;
            }
            if (txtPassword.Text.Length == 0)
            {
                MessageBox.Show("Password not null or not empty");
                txtPassword.Focus();
                return true;
            }
            return false;
        }
        #endregion

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Enter))
            {
                if (!IsNullTextBox())
                {
                    _persenter.AuthenticateUser();
                    return true;
                }
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}
